package com.aaa.ssm.service.impl;

import com.aaa.ssm.dao.UserDao;
import com.aaa.ssm.entity.User;
import com.aaa.ssm.service.UserService;
import com.aaa.ssm.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Resource  //依赖注入
    private UserDao userDao;
    // 依赖注入 用于给添加修改赋予名字
    @Resource
   private HttpSession session;

    /**
     * 登录方法
     * @param user
     * @return
     */
    @Override
    public User checkUser(User user) {
        System.out.println("user = ----------------------------------= " + user);
        User userList = userDao.checkUser(user);
        String oldPassword = user.getPassword();
        System.out.println("userList ----------------------------------= " + userList);
        System.out.println("oldPassword---------------------------------- = " + oldPassword);
        //获取盐值
        String salt = userList.getSalt();
        System.out.println("salt ------------------------------------------= " + salt);
        //生成加密密码

        Sha512Hash sha512Hash = new Sha512Hash(oldPassword,salt,1024);
        System.out.println("sha512Hash ------= " + sha512Hash);
        //比对加密过后的密码
        if (userList.getPassword().equals(sha512Hash.toString())){
            return userList;
        }
        return null;
    }
    /**
     * 分页带参查询
     *
     * @param page
     * @return
     */
    @Override
    public PageInfo queryPage(Page<User> page) {
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        return new PageInfo(userDao.queryByParam(page.getData()));
    }

    /**
     * 分装格式化时间类
     * @return
     */
    private static String time(){
        //获取当前时间
        Date date = new Date();
        //实例化格式化时间类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //format格式化时间
        String format = simpleDateFormat.format(date);
        return format;
    }

    /**
     * 根据编号查询数据
     * @param userId
     * @return
     */
    @Override
    public User queryById(int userId) {
        List<User> userList = userDao.queryById(userId);
        if (userList != null && userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }

    /**
     * 添加数据
     * @param user
     * @return
     */
    @Override
    public int add(User user) {
        //获取设置的loginA值 并进行转换
        String loginA = (String) session.getAttribute("loginA");
        String oldPassword = "123456";
        //获取盐值
        String salt = UUID.randomUUID().toString();
        System.out.println("salt ===================================================================== " + salt);
        //生成加密密码
        Sha512Hash sha512Hash = new Sha512Hash(oldPassword,salt,1024);
        user.setSalt(salt);
        user.setPassword(sha512Hash.toString());
        //将登录人名字赋值
        user.setCreateBy(loginA);
        //将时间赋值
        user.setCreateTime(time());
        //调用dao层的添加方法
        return userDao.add(user);
    }

    /**
     * 更新数据
     * @param user
     * @return
     */
    @Override
    public int update(User user) {
        //异常处理
        try{
            //获取设置的loginA值 并进行转换
            String loginA = (String) session.getAttribute("loginA");
            //将登录人名字赋值
            user.setUpdateBy(loginA);
            //将时间赋值
            user.setUpdateTime(time());
            //调用dao层的更新方法
            return userDao.update(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据编号删除数据
     *
     * @param userId
     * @return
     */
    @Override
    public int deleteById(int userId) {
        return userDao.deleteById(userId);
    }

    @Override
    public int updatePw(User user) {
        String oldPassword = user.getPassword();
        //生成盐值
        String salt = UUID.randomUUID().toString();
        //生成加密密码
        Sha512Hash sha512Hash = new Sha512Hash(oldPassword,salt,1024);
        //设置加密过后的密码
        user.setPassword(sha512Hash.toString());
        user.setSalt(salt);
        user.setUpdateTime(time());
        return userDao.updatePw(user);
    }
}
